<?php
/*
Plugin Name: Flickr Header Spread
Plugin URI: http://www.jaybowalkin.com/plugins/
Description: The header image spread using photos from flickr
Author: Bryan Clark
Version: 0.1
Author URI: http://clarkbw.net/blog/


Changelog

*/

# the JayBo default feed url, http://www.flickr.com/photos/jaybowalkin/tags/jaybo/
$d_rss_icon_feed_url = "http://api.flickr.com/services/feeds/photos_public.gne?id=8882003@N04&tags=jaybo&lang=en-us&format=rss_200";

function jaybo_flickr_header_add_page() {
        add_options_page("JayBo Flickr Header", "Flickr Header", 10, 
                         "jaybo_flickr_header", jaybo_flickr_header_admin);
}

add_action('admin_menu', 'jaybo_flickr_header_add_page');


function jaybo_flickr_header_admin() {

  if ( ! $jaybo_flickr_header_rss ) {
    $jaybo_flickr_header_rss = get_option('jaybo_flickr_header_rss');
    if ( ! $jaybo_flickr_header_rss ) {
      $jaybo_flickr_header_rss = $d_rss_icon_feed_url;
    }
  }

  if ( isset($_POST['jaybo_flickr_submit']) ) {
    if ( function_exists('current_user_can') && !current_user_can('manage_options') )
      die(__('Cheatin&#8217; uh?'));

    $jaybo_flickr_header_rss = $_POST['jaybo_flickr_header_rss'];
    update_option('jaybo_flickr_header_rss', $jaybo_flickr_header_rss);
  }


?>
<?php if ( !empty($_POST ) ) : ?>
        <div id="message" class="updated fade"><p><strong><?php _e('Options saved.') ?></strong></p></div>
<?php endif; ?>

<div class="wrap">
  <h2>Flickr Header Feed</h2>
  <p>Look to the bottom of any flick page for the latest Feed link, copy and past that link into here.  By default all your author links will be diverted to pictures of <a href="http://www.flickr.com/photos/
jaybowalkin/tags/jaybo/">JayBo</a>.</p>

        <form action="" method="post" id="jaybo-flickr-header-conf" >
        <div>Enter your Flickr RSS here: <input type="text" name="jaybo_flickr_header_rss" id="jaybo_flickr_header_rss" value="<?php echo $jaybo_flickr_header_rss; ?>" size="100"/><input type="submit" name="jaybo_flickr_submit" value="Save"/></div>
        </form>
</div>
<?php

}

function jaybo_flickr_header($alt = "") {

	if( file_exists( ABSPATH . WPINC . '/rss.php') ) {
		require_once(ABSPATH . WPINC . '/rss.php');
	} else {
		require_once(ABSPATH . WPINC . '/rss-functions.php');
	}

	$jaybo_flickr_header_rss = get_option('jaybo_flickr_header_rss');
  if ( $jaybo_flickr_header_rss == false ) {
      $jaybo_flickr_header_rss = $d_rss_icon_feed_url;
  }
	
	$rss = fetch_rss( $jaybo_flickr_header_rss );

	if( is_array( $rss->items ) ) {
    ?><a class="more-photos" href="<?php echo strip_tags( $flickr_home ) ?>">More<br/>Photos</a><?php
		$out = '';
		$items = array_slice( $rss->items, 0, 10 );
		while( list( $key, $photo ) = each( $items ) ) {
			if ($key == 0) continue;
			preg_match_all("/<IMG.+?SRC=[\"']([^\"']+)/si",$photo[ 'description' ],$sub,PREG_SET_ORDER);
			$photo_url = $sub[0][1];
			$out .= "<a class='header' href='{$photo[ 'link' ]}'><img class='header' alt='".wp_specialchars( $photo[ 'title' ], true )."' title='".wp_specialchars( $photo[ 'title' ], true )."' src='$photo_url' border='0'></a>";
		}
		$flickr_home = $rss->channel[ 'link' ];
		$flickr_more_title = $rss->channel[ 'title' ];

    echo $out;

	} else {

    echo $alt;

  }
}

?>
